<?php 
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class qlsp_controller extends CI_Controller {
		public function __construct(){    
			parent::__construct();
			$this->load->helper("url");    
		}
		function index()
		{
			session_start();
			$this->load->library('util');
			//$this->load->library('firephp');
			
			if($this->util->checkLogin()==false) {
				return;
			}
			$mataikhoan = $_SESSION['mataikhoan'];
			
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$this->util->connect();
			$this->load->model('sanpham_model');

			$data['sanpham'] = $this->sanpham_model->getSanPham($mataikhoan);
			$data['loaispcha'] = $this->sanpham_model->getLoaiSPCha();
			$this->load->view('qlsp_view.php', $data);
			//$this->firephp->log($_SESSION['tenanh']);
			$_SESSION['thongbao'] = "";
		}
		
		function getSanPham_ajax()
		{
			session_start();
			$this->load->library('util');
			
			if($this->util->checkLogin()==false) {
				return;
			}
			$mataikhoan = $_SESSION['mataikhoan'];
			
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$loai = $_POST['loaicha'];
			
			$this->util->connect();
			$this->load->model('sanpham_model');

			$data['sanpham'] = $this->sanpham_model->getSanPham_ajax($mataikhoan,$loai);

			$this->load->view('qlsp_view_ajax_sp.php', $data);
		}
		
		function getSanPham_ajax_con()
		{
			session_start();
			$this->load->library('util');
			
			if($this->util->checkLogin()==false) {
				return;
			}
			$mataikhoan = $_SESSION['mataikhoan'];
			
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$loai = $_POST['loaicon'];
			$loai_cha = $_POST['loaicha'];
			
			$this->util->connect();
			$this->load->model('sanpham_model');

			$data['sanpham'] = $this->sanpham_model->getSanPham_ajax_con($mataikhoan,$loai,$loai_cha);

			$this->load->view('qlsp_view_ajax_sp.php', $data);
		}
		
		function getLoaiSPCon()
		{
			session_start();
			$this->load->library('util');
			
			//if($this->util->checkLogin()==false) {
				//return;
			//}
			//$mataikhoan = 1;
			
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$loaicha = $_POST['loaicha'];
			
			$this->util->connect();
			$this->load->model('sanpham_model');

			$data['loaicon'] = $this->sanpham_model->getLoaiSPCon($loaicha);

			$this->load->view('qlsp_view_ajax.php', $data);
		}
		
		function dangsanpham()
		{
			session_start();
			$this->load->library('util');
			
			if($this->util->checkLogin()==false) {
				return;
			}
			$mataikhoan = $_SESSION['mataikhoan'];
			
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$this->util->connect();
			$this->load->model('sanpham_model');

			$data['loaispcha'] = $this->sanpham_model->getLoaiSPCha();
			$data['sptam'] = $this->sanpham_model->getSPTam($mataikhoan);
			$this->load->view('themsanpham_view.php', $data);
			
			if($this->input->post(dangsp) && $this->input->post(ten)!="" && $this->input->post(gia)!="" && $this->input->post(editor1)!="" && $this->input->post(loaicha)!="1" && $_FILES['hinh_anh']['name'] !=""){
				$ten = $this->input->post(ten);
				$noidung = strip_tags($this->input->post(editor1));
				$loaicha = $this->input->post(loaicha);
				$loaicon = $this->input->post(loaispcon);
				$gia = $this->input->post(gia);
				$hinhanh = $_FILES['hinh_anh']['name'];
				//$_SESSION['tenanh'] = $_FILES['hinh_anh']['name'];
				
				$this->sanpham_model->do_upload();
				$this->sanpham_model->dangsanpham($mataikhoan,$ten,$noidung,$loaicha,$loaicon,$gia,$hinhanh);
				redirect(base_url()."index.php/qlsp_controller");
			}
		}
		
		public function autoSave()
		{
			session_start();
			$this->load->library('util');
			
			if($this->util->checkLogin()==false) {
				return;
			}
			$mataikhoan = $_SESSION['mataikhoan'];
	
			$this->util->connect();
			
			if($this->input->post('data')){
				$tensp = $this->input->post('tensp');
				$mota = $this->input->post('data');
				$this->load->model('sanpham_model');
				$this->sanpham_model->autoSave($tensp, $mota, $mataikhoan);
			}
	
		}
		
		public function suasanpham($masanpham){
			session_start();
			$this->load->library('util');
			
			if($this->util->checkLogin()==false) {
				return;
			}
			$mataikhoan = $_SESSION['mataikhoan'];
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$this->util->connect();
			$this->load->model('sanpham_model');
			
			$data['dsspcha'] = $this->sanpham_model->getDSSPCha();
			$data['chuyenmuccha'] = $this->sanpham_model->getChuyenMucChaSP($masanpham);
			$cha = $data['chuyenmuccha'];
			
			$data['dsspcon'] = $this->sanpham_model->getDSSPCon($cha);
			$data['chuyenmuccon'] = $this->sanpham_model->getChuyenMucConSP($masanpham);
			$data['sanpham'] = $this->sanpham_model->getSanPhamSua($mataikhoan,$masanpham);

			$this->load->view('suasanpham_view',$data);
		}
		
		function thuchiensua($masanpham)
		{
			session_start();
			$this->load->library('util');
			
			if($this->util->checkLogin()==false) {
				return;
			}
			
			$mataikhoan = $_SESSION['mataikhoan'];
			
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$this->util->connect();
			$this->load->model('sanpham_model');

			if($this->input->post(suasp) && $this->input->post(ten)!="" && $this->input->post(gia)!="" && $this->input->post(editor1)!="" && $this->input->post(loaicha)!="1"){
				$ten = $this->input->post(ten);
				$noidung = strip_tags($this->input->post(editor1));
				$loaicha = $this->input->post(loaicha);
				$loaicon = $this->input->post(loaispcon);
				$gia = $this->input->post(gia);
				$anh_cu = $this->input->post(link_anh);
				$hinhanh = $_FILES['hinh_anh']['name'];
				//$_SESSION['tenanh'] = $_FILES['hinh_anh']['name'];

				$this->sanpham_model->do_upload();
				$this->sanpham_model->suasanpham($masanpham,$mataikhoan,$ten,$noidung,$loaicha,$loaicon,$gia,$hinhanh,$anh_cu);		
				redirect(base_url()."index.php/qlsp_controller");	
			}else{
				$data['ten'] = base_url()."images/product/".$this->input->post(link_anh);
				$this->load->view('testStr',$data);
			}
		}
		
		public function xoasanpham($masanpham){
			session_start();
			$this->load->library('util');
			
			//if($this->util->checkLogin()==false) {
				//return;
			//}
			//$mataikhoan = 1;
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$this->util->connect();
			$this->load->model('sanpham_model');
			
			if($this->sanpham_model->xoasanpham($masanpham) == false){
				show_error("Rất tiếc! Có lỗi đã xảy ra!");
			}else{
				redirect(base_url()."index.php/qlsp_controller");
			}
		}
		
		public function hethang(){
			session_start();
			$this->load->library('util');
			
			//$mataikhoan = 1;
			$data['bar'] = $this->load->view('bar_view',null,true);
			$data['sidemenu'] = $this->load->view('sidemenu_view',null,true);
			
			$this->util->connect();
			$this->load->model('sanpham_model');
			
			$masanpham = $_POST['masp'];
			$this->sanpham_model->hethang($masanpham);
			$this->load->view('hethang_ajax.php');
		}
	}